<template>
  <div>
    <!-- Form -->

    <el-dialog
      :title="info.isAdd ? '添加' : '修改'"
      :visible.sync="info.isshow"
      @close="close"
    >{{form}}
      <el-form :model="form">
        <el-form-item label="所属部门" :label-width="formLabelWidth">
          <el-select
            v-model="form.roleid"
            placeholder="请选择"
            
          >
            
            <el-option :label="item.rolename" :value="item.id" v-for="item in tree" :key="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-switch
            v-model="form.status"
            inactive-text="状态"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="add">{{
        info.isAdd ? "添加" : "修改"
      }}</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { requseradd, requserinfo, requseredit, reqrolelist } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info", "arr"],
  data() {
    return {
      form: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
    tree:[],
      dialogFormVisible: true,

      formLabelWidth: "120px",
    };
  },
  //请求角色数据 遍历所属角色
  mounted() {
    reqrolelist().then(res=>{
      if(res.data.code==200){
        this.tree=res.data.list
      }
    })
  },
  methods: {
    setForm() {
      this.form = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    //请求列表数据
    add() {
      if (this.info.isAdd) {
        requseradd(this.form).then((res) => {
          if (res.data.code == 200) {
            //弹成功
            successAlert(res.data.msg);
            //清空form
            this.setForm();
            //弹框消失
            this.info.isshow = false;
            //列表刷新
            this.$emit("init");
          }
        });
      } else {
        requseredit(this.form).then((res) => {
          if (res.data.code == 200) {
            //弹成功
            successAlert(res.data.msg);
            //清空form
            this.setForm();
            //弹框消失
            this.info.isshow = false;
            //列表刷新
            this.$emit("init");
          }
        });
      }
    },
    //请求一条要修改的数据
    getOne(id) {
      requserinfo({ uid: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.uid = id;
        }
      });
    },
    //如果是编辑的蒙层关闭了 清空form
    close() {
      if (this.info.isAdd == false) {
        this.setForm();
      }
    },
  },
};
</script>

<style>
.el-switch {
  margin-left: 80px;
}
</style>
